package com.yst.gateway.monitor.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.groups.Default;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.yst.gateway.monitor.common.beans.Response;
import com.yst.gateway.monitor.common.utils.LoginUserCache;
import com.yst.gateway.monitor.common.utils.MessageUtils;
import com.yst.gateway.monitor.common.validator.groups.Groups;
import com.yst.gateway.monitor.models.SysMenus;
import com.yst.gateway.monitor.models.SysUsers;
import com.yst.gateway.monitor.services.SysMenuService;
import com.yst.gateway.monitor.services.SysUserInterfaceService;
import com.yst.gateway.monitor.services.SysUserService;

/**
 * 前置系统用户管理访问控制类
 * @author sheungxin
 *
 */
@RestController
@RequestMapping(value = "/sys/users")
public class SysUserController {
	
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysMenuService sysMenuService;
	@Autowired
	private SysUserInterfaceService service;
	
//	@RequestMapping(value = "/add",method = RequestMethod.POST)
//	public Response post(@RequestBody @Validated({Default.class,Groups.Add.class}) SysUsers sysUser){
//		Response response=new Response();
//		if(sysUserService.add(sysUser)){
//			response.success();
//		}else{
//			response.failure();
//		}
//		return response;
//	}
	
	@RequestMapping(value = "/modify",method = RequestMethod.PUT)
	public Response put(@RequestBody @Validated({Default.class,Groups.Modify.class}) SysUsers sysUser){
		Response response=new Response();
		if(sysUserService.update(sysUser)){
			response.success();
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/lock/{id}",method = RequestMethod.PUT)
	public Response lock(@PathVariable("id") String id){
		Response response=new Response();
		if(sysUserService.updateStatusById(id)){
			response.success();
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public Response get(@PathVariable("id") String id) {
		Response response=new Response();
		SysUsers user=sysUserService.getById(id);
		if(user!=null){
			response.success(user);
		}else{
			response.failure();
		}
		return response;
	}
	
	
	@RequestMapping(value = "/getByPage", method = RequestMethod.GET)
	public Response getByPage(@RequestParam Map<String, Object> paramMap) {
		Response response=new Response();
		int pageNum=1;
		int pageSize=10;
		try{
			if(paramMap.containsKey("pageNum")){
				pageNum=Integer.parseInt((String)paramMap.get("pageNum"));
				paramMap.remove("pageNum");
			}
			if(paramMap.containsKey("pageSize")){
				pageSize=Integer.parseInt((String)paramMap.get("pageSize"));
				paramMap.remove("pageSize");
			}
		}catch (Exception e) {
		}
		response.success(sysUserService.getByPage(pageNum, pageSize, paramMap));
		return response;
	}
	
	@RequestMapping(value = "/del/{id}",method = RequestMethod.PUT)
	public Response del(@PathVariable("id") String id){
		Response response=new Response();
		if(sysUserService.delById(id)){
			response.success();
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/checkName", method = RequestMethod.GET)
	public boolean checkName(@RequestParam String sysUserName) {
		return sysUserService.getByName(sysUserName)==null?true:false;
	}
	
//	@RequestMapping(value = "/loginIn",method = RequestMethod.POST)
//	public Response loginIn(@RequestParam String name,@RequestParam String password){
//		Response response=new Response();
//		SysUsers sysUser=sysUserService.getByName(name);
//		if(sysUser!=null){
//			if(sysUser.getSysUserDel()==0){
//				response.failure(MessageUtils.getMessage("user.loginIn.invalid"));
//			}else if(sysUser.getSysUserStatus()==0){
//				response.failure(MessageUtils.getMessage("user.loginIn.lock"));
//			}else if(!new Md5PasswordEncoder().isPasswordValid(sysUser.getSysUserPsw(), password, sysUser.getSysUserId())){
//				response.failure(MessageUtils.getMessage("user.loginIn.error"));
//			}else{
//				LoginUserCache.cacheUserInfo(sysUser);
//				Map<String, Object> paramMap=new HashMap<String, Object>();
//				paramMap.put("status", "1");
//				try {
//					LoginUserCache.cacheUserMenus(sysMenuService.getTreeByMap(paramMap));
//					response.success();
//				} catch (JsonProcessingException e) {
//					response.failure(MessageUtils.getMessage("user.loginIn.resource"));
//				}
//			}
//		}else{
//			response.failure(MessageUtils.getMessage("user.loginIn.notExists").replace("{0}", name));
//		}
//		return response;
//	}

//	@RequestMapping(value = "/loginOut",method = RequestMethod.GET)
//	public Response loginOut(){
//		Response response=new Response();
//		LoginUserCache.invalidUser();
//		response.success();
//		return response;
//	}
	
	/**
	 * 注册用户
	 */
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public Response post(@RequestBody @Validated({Default.class,Groups.Add.class}) SysUsers sysUser){
		Response response=new Response();
		Map<String, Object> result = service.registerUser(sysUser);
		if(result.get("Success").equals("true")){
			sysUser.setSysUserId(result.get("UserUniqueCode").toString());
			if(sysUserService.add(sysUser)) {
				response.success();
			}else {
				response.failure(result.get("Message").toString());
			}
		}else{
			response.failure("1", result.get("Message").toString());
		}
		return response;
	}
	
	/**
	 * 用户登录
	 * @param name
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "/loginIn",method = RequestMethod.POST)
	public Response loginIn(@RequestParam String name,@RequestParam String password){
		Response response = new Response();
		SysUsers sysUser = service.getByUser(name, password);
		SysUsers user = sysUserService.getById(sysUser.getSysUserId());
		if(sysUser.getSysUserId()!=null){
			if(sysUser.getSysUserDel() == 0){
				response.failure(MessageUtils.getMessage("user.loginIn.invalid"));
			}else{
				if(user == null) {
					sysUserService.add(sysUser);
				}else if(user.getSysUserStatus() == 0){
					response.failure(MessageUtils.getMessage("user.loginIn.lock"));
				}else {
					LoginUserCache.cacheUserInfo(sysUser);
					Map<String, Object> paramMap=new HashMap<String, Object>();
					paramMap.put("status", "1");
					try {
						List<SysMenus> menulist = sysMenuService.getTreeByMap(paramMap);
						LoginUserCache.cacheUserMenus(sysMenuService.getTreeByMap(paramMap));
						response.success();
					} catch (JsonProcessingException e) {
						response.failure(MessageUtils.getMessage("user.loginIn.resource"));
					}
				}
			}
		}else{
			response.failure(sysUser.getMessage());
		}
		return response;
	}
	
	/**
	 * 登录退出
	 * @return
	 */
	@RequestMapping(value = "/loginOut",method = RequestMethod.GET)
	public Response loginOut(){
		Response response=new Response();
		Map<String, Object> result = service.loginOut();
		if(result.get("Message").equals("loginout")) {
			response.failure();
			return response;
		}else {
			if(result.get("Success").equals("true")) {
				LoginUserCache.invalidUser();
				response.success();
				return response;
			}else {
				response.failure();
				return response;
			}
		}
	}
	
}
